﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace NurseStation.common
{
    /// <summary>
    /// 指定任意控件为拖动控制，控制指定窗体拖动
    /// </summary>
    public class DragBehavior : Behavior<FrameworkElement>
    {
        /// <summary>
        /// 需要拖动的窗体
        /// </summary>
        public Window Win
        {
            get { return (Window)GetValue(WinProperty); }
            set { SetValue(WinProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Win.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WinProperty =
            DependencyProperty.Register("Win", typeof(Window), typeof(DragBehavior), new PropertyMetadata(null));



        /// <summary>
        ///附加事件
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;

        }
        /// <summary>
        /// 移除事件
        /// </summary>
        protected override void OnDetaching()
        {
            AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObject_PreviewMouseLeftButtonDown;
            //DragMove();
        }

        /// <summary>
        /// 鼠标在图片上点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (Win!=null )
            {
                if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
                {
                    Win.DragMove();
                }
            }
        }
    }
}
